#!/usr/bin/env bash

set -v

export LFS='/mnt/lfs'

sudo mkdir -pv $LFS/tools
sudo ln -fsv $LFS/tools /

# 添加 LFS 用户
id lfs &> /dev/null
if [ $? = 0 ]; then sudo userdel lfs; fi
if grep -q "^lfs:" /etc/group; then sudo groupdel lfs; fi
sudo rm -rf /home/lfs

sudo groupadd lfs
sudo useradd -s /bin/bash -g lfs -m -k /dev/null lfs
sudo passwd lfs

sudo chown -v lfs $LFS/tools
sudo chown -v lfs $LFS/sources

sudo tee /home/lfs/.bash_profile << "EOF"
exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash
EOF

sudo tee /home/lfs/.bashrc << "EOF"
set +h
umask 022
LFS=/mnt/lfs
LC_ALL=POSIX
LFS_TGT=$(uname -m)-lfs-linux-gnu
PATH=/tools/bin:/bin:/usr/bin
export LFS LC_ALL LFS_TGT PATH

alias ls="ls --color=tty"
alias ll="ls -alh --color=tty"
EOF

sudo chown lfs:lfs /home/lfs/.bash_profile /home/lfs/.bashrc
